

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proyectoso;
import java.util.concurrent.Semaphore;

/**
 *
 * @author Christian
 */
public class Bote {
    int isla;
    BoatGrader b;
    private int children;
    int adult;
    Bote(int is){
        this.isla=is;
        this.b=new BoatGrader();
        this.children=0;
        this.adult=0;
    }
    public synchronized void TomarBoteChild(Child ch){
        if (children<1 && adult==0 && ch.GetIsla()==isla){
            children++;
            ch.SetFlag(true);
        }
    }
    public synchronized void TomarBoteChild2(Child ch){
            if (children<2 && adult==0 && ch.GetIsla()==isla){
                    children++;
                if(children==1){
                    ch.passenger=true;
                }
                ch.SetFlag(true);
            }
    }
    public synchronized void TomarBoteAdult(Adult ad){
        if (adult==0 && children==0 && ad.GetIsla()==isla){
            adult++;
            ad.SetFlag(true);
        }
    }
    public synchronized void ViajarChild(Child ch){
        if(ch.GetFlag()){
            if(ch.GetIsla()==0){
                ch.SetIsla(1);
                if (ch.passenger){
                    b.ChildRideToMolokai();
                    ch.passenger=false;
                }
                else
                    b.ChildRowToMolokai();
                ch.SetFlag(false);
                children--;
                isla=1;
                ch.ar.Escribir(ch.isla, ch.persona);
            }
            else{
                ch.SetIsla(0);
                if (ch.passenger){
                    b.ChildRideToOahu();
                    ch.passenger=false;
                }
                else
                    b.ChildRowToOahu();
                ch.SetFlag(false);
                children--;
                isla=0;
                ch.ar.Escribir(ch.isla, ch.persona);
            }
        }
    }
    public synchronized void ViajarAdulto(Adult ad){
        if(ad.GetFlag()){
            ad.ar.Borrar(ad.GetIsla(), ad.GetPerson());
            if(ad.GetIsla()==0){
                ad.SetIsla(1);
                b.AdultRowToMolokai();
                ad.SetFlag(false);
                adult--;
                isla=1;
                ad.ar.Escribir(ad.isla, ad.persona);
            }
            else{
                ad.SetIsla(0);
                b.AdultRowToOahu();
                ad.SetFlag(false);
                adult--;
                isla=0;
                ad.ar.Escribir(ad.isla, ad.persona);
            }
        }
    }
    public int GetChildren(){
        return this.children;
    }
}